<!DOCTYPE html>
<html lang="zh">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  
  
  <title>Lua元表 | 舛羽的笔记小站</title>
  <meta name="author" content="cy" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="Lua" />
  
  <meta name="description" content="Lua中拥有一个不同于其他语言的特性叫元表，元表拥有一些类似于面向对象的特点，一个表可以将另外一个表作为自己的元表，同时提供了一些方法用于处理表与表之间的关系，这会为我们实现面向对象提供基础。">
<meta property="og:type" content="article">
<meta property="og:title" content="Lua元表">
<meta property="og:url" content="http://example.com/2023/07/13/Lua%E8%AF%AD%E6%B3%95/Lua%E5%85%83%E8%A1%A8/index.html">
<meta property="og:site_name" content="舛羽的笔记小站">
<meta property="og:description" content="Lua中拥有一个不同于其他语言的特性叫元表，元表拥有一些类似于面向对象的特点，一个表可以将另外一个表作为自己的元表，同时提供了一些方法用于处理表与表之间的关系，这会为我们实现面向对象提供基础。">
<meta property="og:locale">
<meta property="og:image" content="https://s2.loli.net/2023/07/13/Ndqi5uICYwEDszc.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/13/qeKVB5RJUWSOkN8.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/13/yiUgsPtZbdeKY7q.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/13/v8xoecVNB5XJE6Z.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/13/xZj5Y4Ka3TrUkXt.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/13/rsXM3naK45hc8xV.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/13/5WyTvLEQkGrY2zn.png">
<meta property="article:published_time" content="2023-07-13T06:36:04.000Z">
<meta property="article:modified_time" content="2023-07-13T09:39:11.457Z">
<meta property="article:author" content="cy">
<meta property="article:tag" content="Lua">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2023/07/13/Ndqi5uICYwEDszc.png">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/css/kratosr.min.css" media="all"></script>
  
    <link rel="stylesheet" id="darkmode-css" href="/css/kr-color-dark.min.css" media="(prefers-color-scheme: dark)"></script>
    <script src="/js/kr-dark.min.js"></script>
  
  
    <link rel="stylesheet" id="highlight-css" href="/css/highlight/night-eighties.min.css" media="all"></script>
  
  
  <link rel="stylesheet" id="fontawe-css" href="/vendors/font-awesome@4.7.0/css/font-awesome.min.css" media="all"></script>
  <link rel="stylesheet" id="nprogress-css" href="/vendors/nprogress@0.2.0/nprogress.css" media="all"></script>
  
  
  
    <link rel="stylesheet" href="/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css"></script>
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="/vendors/jquery@3.6.0/dist/jquery.min.js"></script>
  
    <script src="/vendors/qrcode_js@1.0.0/qrcode.min.js"></script>
  
  
  <style>
    
      .kratos-cover.kratos-cover-2 {
        background-image: url('/images/banner.webp');
      }
    
    
      @media(min-width:768px) {
        body.custom-background {
          background-image: url('/images/bg.webp');
        }
      }
    
  </style>
  
<meta name="generator" content="Hexo 7.3.0"></head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <header id="kratos-desktop-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="nav-header">
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <header id="kratos-mobile-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="color-logo"><a href="/">舛羽的笔记小站</a></div>
                    <div class="nav-toggle">
                        <a class="kratos-nav-toggle js-kratos-nav-toggle">
                            <i></i>
                        </a>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/">
                        <h2>舛羽的笔记小站</h2> <br />
                        <span></span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div id="main" class="row">
                    

        

            <section class="col-md-8">

        

            <article itemscope itemtype="https://schema.org/Article">
    
    <link itemprop="mainEntityOfPage" href="http://example.com/2023/07/13/Lua%E8%AF%AD%E6%B3%95/Lua%E5%85%83%E8%A1%A8/">
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            
                <h1 class="kratos-entry-title text-center" itemprop="name headline">Lua元表</h1>
            
            
            <ul class="kratos-post-meta text-center">
                <li><time datetime="2023-07-13T06:36:04.000Z" itemprop="datePublished"><i class="fa fa-calendar"></i> 2023-07-13</time></li>
                <li itemprop="author" itemscope itemtype="https://schema.org/Person">
                    <i class="fa fa-user"></i> 作者 <span itemprop="name">cy</span>
                </li>
                <li>
                    <i class="fa fa-edit"></i> 
                    
                    
                        ~2.80K
                    
                    字
                </li>
                
            </ul>
        </header>
        <div class="kratos-post-content">
            
            <div id="expire-alert" class="alert alert-warning hidden" role="alert">
                <div class="icon"><i class="fa fa-warning"></i></div>
                <div class="text"><p>本文最后编辑于 <time datetime="1689241151457"></time> 前，其中的内容可能需要更新。</p></div>
            </div>
            
            
            
            <hr />
            <div itemprop="articleBody"><p>Lua中拥有一个不同于其他语言的特性叫元表，元表拥有一些类似于面向对象的特点，一个表可以将另外一个表作为自己的元表，同时提供了一些方法用于处理表与表之间的关系，这会为我们实现面向对象提供基础。</p>
<span id="more"></span>
<h2 id="第七章-Lua元表"><a href="#第七章-Lua元表" class="headerlink" title="第七章 Lua元表"></a>第七章 Lua元表</h2><h3 id="7-1-元表概念"><a href="#7-1-元表概念" class="headerlink" title="7.1 元表概念"></a>7.1 元表概念</h3><p>我们可以对其他的变量进行操作，但是无法对两个table进行操作。所以Lua提供了元表，允许我们改变table行为，每个行为关联了对应的元方法。</p>
<p>任何表变量都可以有自己的元表。</p>
<p>当我们子表进行一些特定操作时，会执行元表的内容。</p>
<h3 id="7-2-设置元表"><a href="#7-2-设置元表" class="headerlink" title="7.2 设置元表"></a>7.2 设置元表</h3><p>元表的设置有一个固定的方法，为setmetatable，用法如下：</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">PokeMon=&#123;&#125;;</span><br><span class="line">Eevee=&#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">setmetatable</span>(Eevee,PokeMon);</span><br></pre></td></tr></table></figure>
<h3 id="7-3-tostring"><a href="#7-3-tostring" class="headerlink" title="7.3 tostring"></a>7.3 tostring</h3><p>在元表中我们可以设计tostring方法用于规定子表的输出行为。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">PokeMon=&#123;</span><br><span class="line">    <span class="built_in">__tostring</span>=<span class="function"><span class="keyword">function</span><span class="params">(t)</span></span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;这个宝可梦叫做&quot;</span>..t.name;</span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line">&#125;;</span><br><span class="line">Eevee=&#123;</span><br><span class="line">    name=<span class="string">&quot;Eevee&quot;</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">setmetatable</span>(Eevee,PokeMon);</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(Eevee);</span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/13/Ndqi5uICYwEDszc.png" alt=""></p>
<p>可以看到，子表输出的时候会用父表的tostring方法去输出。</p>
<p>同时这个函数传入的第一个参数一定是子表本身，如果需要再传参数，需要再另加参数。</p>
<p><strong>重点：</strong>只是在直接输出的时候会进行替换，不能使用..直接去连接table，会报错。</p>
<h3 id="7-4-call"><a href="#7-4-call" class="headerlink" title="7.4 call"></a>7.4 call</h3><p>在Lua中将表作为一个方法进行调用时会使用父表的该方法进行替换。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">PokeMon=&#123;</span><br><span class="line">    <span class="built_in">__call</span>=<span class="function"><span class="keyword">function</span><span class="params">(t)</span></span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;就决定是你了&quot;</span>..t.name);</span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line">&#125;;</span><br><span class="line">Eevee=&#123;</span><br><span class="line">    name=<span class="string">&quot;Eevee&quot;</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">setmetatable</span>(Eevee,PokeMon);</span><br><span class="line">Eevee();</span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/13/qeKVB5RJUWSOkN8.png" alt=""></p>
<p>和tostring一样第一个参数是子表自身，如果需要另传参数，需要再加参数。</p>
<h3 id="7-5-运算符重载"><a href="#7-5-运算符重载" class="headerlink" title="7.5 运算符重载"></a>7.5 运算符重载</h3><p>Lua的运算符重载也是基于元表而进行的。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">PokeMon=&#123;</span><br><span class="line">    <span class="built_in">__add</span>=<span class="function"><span class="keyword">function</span><span class="params">(t1,t2)</span></span></span><br><span class="line">        t1.level=t1.level+t2.num;</span><br><span class="line">        <span class="keyword">return</span> t1;</span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line">&#125;;</span><br><span class="line">Eevee=&#123;</span><br><span class="line">    name=<span class="string">&quot;伊布&quot;</span>;</span><br><span class="line">    level=<span class="number">1</span>;</span><br><span class="line">&#125;;</span><br><span class="line">RareCandy=&#123;</span><br><span class="line">    num=<span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">setmetatable</span>(Eevee,PokeMon);</span><br><span class="line">Eevee=Eevee+RareCandy;</span><br><span class="line"><span class="built_in">print</span>(Eevee.name..<span class="string">&quot;升到了&quot;</span>..Eevee.level..<span class="string">&quot;级&quot;</span>);</span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/13/yiUgsPtZbdeKY7q.png" alt=""></p>
<p>可以看到，首先运算符重载的函数需要传入两个参数，这两个参数就是需要运算的表，同时这两个运算的表至少有一个是以有运算符重载的表为元表的。</p>
<p>还有的运算符重载函数包括：</p>
<p><img src="https://s2.loli.net/2023/07/13/v8xoecVNB5XJE6Z.png" alt=""></p>
<h3 id="7-6-index"><a href="#7-6-index" class="headerlink" title="7.6 index"></a>7.6 index</h3><p>一般情况下我们访问一个表中不存在的键时，会返回一个nil，index方法的作用是，如果在当前表中没有这个键，就会把这个去元表的这个方法里寻找这个键，如果找到了就返回元表中的值，否则返回nil。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">PokeMon=&#123;</span><br><span class="line">    <span class="built_in">__index</span>=&#123;isPokemon=<span class="literal">true</span>&#125;;</span><br><span class="line">&#125;;</span><br><span class="line">Eevee=&#123;</span><br><span class="line">    name=<span class="string">&quot;伊布&quot;</span>;</span><br><span class="line">    level=<span class="number">1</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">setmetatable</span>(Eevee,PokeMon);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;伊布是宝可梦吗？&quot;</span>);</span><br><span class="line"><span class="built_in">print</span>(Eevee.isPokemon);</span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/13/xZj5Y4Ka3TrUkXt.png" alt=""></p>
<h3 id="7-7-newindex"><a href="#7-7-newindex" class="headerlink" title="7.7 newindex"></a>7.7 newindex</h3><p>index方法是用于找表中为定义的键，newindex是用来对表更新。</p>
<p>当你给表的一个缺少的索引赋值，解释器就会查找__newindex 元方法：如果存在则调用这个函数而不进行赋值操作。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">PokeMon=&#123;</span><br><span class="line">    <span class="built_in">__newindex</span>=<span class="function"><span class="keyword">function</span><span class="params">(t,key)</span></span></span><br><span class="line">        <span class="keyword">if</span> key==<span class="string">&quot;isPokemon&quot;</span> <span class="keyword">then</span></span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;已经有这个值了&quot;</span>);</span><br><span class="line">        <span class="keyword">end</span></span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line">&#125;;</span><br><span class="line">Eevee=&#123;</span><br><span class="line">    name=<span class="string">&quot;伊布&quot;</span>;</span><br><span class="line">    level=<span class="number">1</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;不定义元表直接赋值&quot;</span>);</span><br><span class="line">Eevee.isPokemon=<span class="literal">true</span>;</span><br><span class="line"><span class="built_in">print</span>(Eevee.isPokemon);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;删除Eevee中的isPokemon值&quot;</span>);</span><br><span class="line">Eevee.isPokemon=<span class="literal">nil</span>;</span><br><span class="line"><span class="built_in">print</span>(Eevee.isPokemon);</span><br><span class="line"></span><br><span class="line"><span class="built_in">setmetatable</span>(Eevee,PokeMon);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;重新赋值isPokemon&quot;</span>);</span><br><span class="line">Eevee.isPokemon=<span class="literal">true</span>;</span><br><span class="line"><span class="built_in">print</span>(Eevee.isPokemon);</span><br></pre></td></tr></table></figure>
<p>输出的结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/13/rsXM3naK45hc8xV.png" alt=""></p>
<p>如果我们想绕开index或者newindex方法可以使用rawget和rawset方法。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">PokeMon=&#123;</span><br><span class="line">    <span class="built_in">__newindex</span>=<span class="function"><span class="keyword">function</span><span class="params">(t,key)</span></span></span><br><span class="line">        <span class="keyword">if</span> key==<span class="string">&quot;isPokemon&quot;</span> <span class="keyword">then</span></span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;已经有这个值了&quot;</span>);</span><br><span class="line">        <span class="keyword">end</span></span><br><span class="line">    <span class="keyword">end</span>;</span><br><span class="line">    <span class="built_in">__index</span>=&#123;isCute=<span class="literal">true</span>&#125;;</span><br><span class="line">&#125;;</span><br><span class="line">Eevee=&#123;</span><br><span class="line">    name=<span class="string">&quot;伊布&quot;</span>;</span><br><span class="line">    level=<span class="number">1</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">rawget</span>(Eevee,isCute));</span><br><span class="line"><span class="built_in">rawset</span>(Eevee,<span class="string">&quot;isPokemon&quot;</span>,<span class="literal">true</span>);</span><br><span class="line"><span class="built_in">print</span>(Eevee.isPokemon);</span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/13/5WyTvLEQkGrY2zn.png" alt=""></p>
</div>
        </div>
        
            <div class="kratos-copyright text-center clearfix">
                <h5 itemprop="copyrightNotice">本作品采用 <a rel="license nofollow" target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
            </div>
        
        <footer class="kratos-entry-footer clearfix">
            
                <div class="post-like-donate text-center clearfix" id="post-like-donate">
                
                
                    <a class="share" href="javascript:;"><i class="fa fa-share-alt"></i> 分享</a>
                    <div class="share-wrap" style="display: none;">
    <div class="share-group">
        <a href="javascript:;" class="share-plain qq" onclick="share('qq');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-qq"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain qzone" onclick="share('qzone');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-star"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weixin pop style-plain" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weixin"></i>
            </div>
            <div class="share-int">
                <div class="qrcode" id="wechat-qr"></div>
                <p>打开微信“扫一扫”，打开网页后点击屏幕右上角分享按钮</p>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weibo" onclick="share('weibo');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weibo"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain facebook style-plain" onclick="share('facebook');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-facebook"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain twitter style-plain" onclick="share('twitter');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-twitter"></i>
            </div>
        </a>
    </div>
    <script type="text/javascript">
        $(()=>{
            new QRCode("wechat-qr", {
                text: "http://example.com/2023/07/13/Lua%E8%AF%AD%E6%B3%95/Lua%E5%85%83%E8%A1%A8/",
                width: 150,
                height: 150,
                correctLevel : QRCode.CorrectLevel.H
            });
        });
        function share(dest) {
            const qqBase        = "https://connect.qq.com/widget/shareqq/index.html?";
            const weiboBase     = "https://service.weibo.com/share/share.php?";
            const qzoneBase     = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
            const facebookBase  = "https://www.facebook.com/sharer/sharer.php?";
            const twitterBase   = "https://twitter.com/intent/tweet?";
            const hostUrl       = "http://example.com/2023/07/13/Lua%E8%AF%AD%E6%B3%95/Lua%E5%85%83%E8%A1%A8/";
            const title         = "「Lua元表」";
            const excerpt       = `Lua中拥有一个不同于其他语言的特性叫元表，元表拥有一些类似于面向对象的特点，一个表可以将另外一个表作为自己的元表，同时提供了一些方法用于处理表与表之间的关系，这会为我们实现面向对象提供基础。`;
            let _URL;
            switch (dest) {
                case "qq"       : _URL = qqBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";     break;
                case "weibo"    : _URL = weiboBase+"url="+hostUrl+"&title="+title+excerpt;                                 break;
                case "qzone"    : _URL = qzoneBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";  break;
                case "facebook" : _URL = facebookBase+"u="+hostUrl;                                                        break;
                case "twitter"  : _URL = twitterBase+"text="+title+excerpt+"&url="+hostUrl;                                break;
            }
            window.open(_URL);
        };
    </script>
</div>
                
                </div>
            
            <div class="footer-tag clearfix">
                <div class="pull-left">
                <i class="fa fa-tags"></i>
                    <a class="tag-none-link" href="/tags/Lua/" rel="tag">Lua</a>
                </div>
                <div class="pull-date">
                    <time datetime="2023-07-13T09:39:11.457Z" itemprop="dateModified">最后编辑：2023-07-13</time>
                </div>
            </div>
        </footer>
    </div>
    
        <nav class="navigation post-navigation clearfix" role="navigation">
            
            <div class="nav-previous clearfix">
                <a title=" Lua协同程序" href="/2023/07/12/Lua语法/Lua协程/">&lt; 上一篇</a>
            </div>
            
            
            <div class="nav-next clearfix">
                <a title=" Lua面向对象" href="/2023/07/13/Lua语法/Lua面向对象/">下一篇 &gt;</a>
            </div>
            
        </nav>
    
    
</article>

        

            </section>

        

                
            

<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
    <!-- 文章和页面根据splitter来分割，没有的话就从头开始设置为sticky -->
    
    
                <aside id="krw-about" class="widget widget-kratos-about clearfix">
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="/images/avatar.webp" loading="lazy" decoding="auto" />
        </div>
    </div>
    <div class="textwidget">
        <p class="text-center"></p>
    </div>
    <div class="site-meta">
        <a class="meta-item" href="/archives/">
            <span class="title">
                文章
            </span>
            <span class="count">
                39
            </span>
        </a>
        <a class="meta-item" href="/categories/">
            <span class="title">
                分类
            </span>
            <span class="count">
                7
            </span>
        </a>
        <a class="meta-item" href="/tags/">
            <span class="title">
                标签
            </span>
            <span class="count">
                10
            </span>
        </a>
    </div>
</aside>
            
                    <div class="sticky-area">
                
                
  <aside id="krw-categories" class="widget widget-kratos-categories clearfix">
    <h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/C-%E7%9F%A5%E8%AF%86/">C#知识</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Lua/">Lua</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Unity%E7%BD%91%E7%BB%9C%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">Unity网络游戏开发</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/">宝可梦小说</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%AC%94%E8%AE%B0/">操作系统笔记</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AC%94%E8%AE%B0/">数据结构笔记</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E9%9A%8F%E6%83%B3/">随想</a><span class="category-list-count">1</span></li></ul>
  </aside>


            
                
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/tags/C/" style="font-size: 0.75em;">C#</a> <a href="/tags/Lua/" style="font-size: 0.8em;">Lua</a> <a href="/tags/unity/" style="font-size: 0.65em;">unity</a> <a href="/tags/%E5%AE%9D%E5%8F%AF%E6%A2%A6/" style="font-size: 0.7em;">宝可梦</a> <a href="/tags/%E5%BD%B1%E8%AF%84/" style="font-size: 0.6em;">影评</a> <a href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" style="font-size: 0.8em;">操作系统</a> <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" style="font-size: 0.6em;">数据结构</a> <a href="/tags/%E7%AE%97%E6%B3%95/" style="font-size: 0.6em;">算法</a> <a href="/tags/%E7%BD%91%E7%BB%9C%E6%B8%B8%E6%88%8F/" style="font-size: 0.65em;">网络游戏</a> <a href="/tags/%E9%9A%8F%E6%83%B3/" style="font-size: 0.6em;">随想</a>
      </div>
  </aside>

            
                
  <aside id="krw-posts" class="widget widget-kratos-posts">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/2025/10/08/%E9%9A%8F%E6%83%B3/%E3%80%8A%E6%98%9F%E4%B8%89%E5%9B%BD%E3%80%8B%E5%90%90%E6%A7%BD/"><i class="fa  fa-book"></i> 《星三国》吐槽</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/14/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B45/"><i class="fa  fa-book"></i> 宝可梦小说第五章</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/13/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B44/"><i class="fa  fa-book"></i> 宝可梦小说第四章</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/10/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B43/"><i class="fa  fa-book"></i> 宝可梦小说第三章</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/09/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B42/"><i class="fa  fa-book"></i> 宝可梦小说第二章</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

            
    </div>
</section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer"  >
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        
                        
                        
                        
                        
                        
                        
                        
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2025 舛羽的笔记小站 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Site built with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by cy.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://github.io" target="_blank">Github Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="/vendors/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="/vendors/nprogress@0.2.0/nprogress.js"></script>
<script>
    if (!window.kr) {
        window.kr = {};
    }
    window.kr.notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));
    window.kr.siteRoot = "/";
</script>


    <script async src="/js/candy.min.js"></script>




    <script defer src="/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="/vendors/clipboard@2.0.6/dist/clipboard.min.js"></script>
<script defer src="/js/kratosr.min.js"></script>
<script defer src="/js/pjax.min.js"></script>



<!-- Extra support for third-party plguins  -->


    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML
"></script>
</body>
</html>